﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#region   变量类模块
/// <summary>
/// **********************************************************************}
///                   NetCls   自定义网络类模块描述                        }
///   				 Ver:1.0          Author: HeavenBen                   }
///                   Last Version:Ver:1.0.0B                             }
///      Create Date:2011-09-21        Ex. Date: 2011-09-21               }
///-----------------------------------------------------------------------}
///    Description:                                                       }
///     请以此命名约定来规范整个系统框架                                    }
///     1.所有的模块工程前缀为Thinking.来命名                               }
///     2.所有功能性的涵盖空间命名以.之后的命名来规范。                      }
///     3.方法、属性等名称尽量以简约、易懂的方法来命名。                     }
///     Update Detail List:                                               }
/// Update Version    |   Last ModifyDate   |   Modify Person  |   Remark }
///                                                                       }
///-----------------------------------------------------------------------}
///		    CopyRight (C) 2011  Thinking Co.Ltd.                      	  }
///***********************************************************************}
/// </summary>
#endregion
namespace Thinking.Core
{
    class NetCls
    {
        #region 根据指定的目标IP地址返回Time的等级。
        /// <summary>
        /// 根据指定的目标IP地址返回Time时间。
        /// </summary>
        /// <param name="strIpOrDName">指定的目标IP字符串</param>
        /// <returns>一个网络周期时间的等级</returns>
        public static int GetPingTime(string strIpOrDName)
        {
            long lTimeOut = 0;
            int iResult = 0;
            try
            {
                System.Net.NetworkInformation.Ping objPingSender = new System.Net.NetworkInformation.Ping();

                System.Net.NetworkInformation.PingOptions objPinOptions = new System.Net.NetworkInformation.PingOptions();
                objPinOptions.DontFragment = true;
                string data = "1111";
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                int intTimeout = 3000;
                System.Net.NetworkInformation.PingReply objPinReply = objPingSender.Send(strIpOrDName);
                lTimeOut = objPinReply.RoundtripTime;

                string strInfo = objPinReply.Status.ToString();
                if (strInfo == "Success")
                {
                    if (lTimeOut <= 10)
                    {
                        iResult = 100;
                    }
                    else if (lTimeOut <= 50)
                    {
                        iResult = 90;
                    }
                    else if (lTimeOut <= 80)
                    {
                        iResult = 80;
                    }
                    else if (lTimeOut <= 100)
                    {
                        iResult = 50;
                    }
                    else if (lTimeOut <= 200)
                    {
                        iResult = 30;
                    }
                    else if (lTimeOut <= 300)
                    {
                        iResult = 20;
                    }
                    else if (lTimeOut <= 400)
                    {
                        iResult = 10;
                    }
                    else if (lTimeOut <= 500)
                    {
                        iResult = 5;
                    }
                }
                else
                {
                    return iResult;
                }
                return iResult;
            }
            catch (Exception)
            {
                return iResult;
            }
        }
        #endregion


    }
}
